package com.lily.aidouzixun.bean;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.SparseArray;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 *  动态详情首页信息
 *
 */
public class DynamicDetailHomeInfo implements Parcelable {

    /**
     * Json-key
     */
    private static final String CURPAGE_JOSANKEY = "curpage";
    private static final String TOTALPAGE_JOSANKEY = "totalpage";
    private static final String COMMENTS_JSONKEY = "comments";

    /**
     * 评论列表
     */
    private int curpage;
    private int totalpage;
    private SparseArray<DynamicCommentInfo> commentList;

    public int getCurpage() {
        return curpage;
    }

    public void setCurpage(int curpage) {
        this.curpage = curpage;
    }

    public int getTotalpage() {
        return totalpage;
    }

    public void setTotalpage(int totalpage) {
        this.totalpage = totalpage;
    }

    public SparseArray<DynamicCommentInfo> getCommentList() {
        return commentList;
    }

    public void setCommentList(SparseArray<DynamicCommentInfo> commentList) {
        this.commentList = commentList;
    }

    /**
     * 解析数据
     * @param jsonObj
     * @return 文章首页数据
     */
    public static DynamicDetailHomeInfo parseInfo(JSONObject jsonObj) {
        if (jsonObj == null) {
            return null;
        }
        DynamicDetailHomeInfo discussDetailHomeInfo = new DynamicDetailHomeInfo();

        if (jsonObj.has(CURPAGE_JOSANKEY)) {
            discussDetailHomeInfo.setCurpage(jsonObj.optInt(CURPAGE_JOSANKEY));
        }
        if (jsonObj.has(TOTALPAGE_JOSANKEY)) {
            discussDetailHomeInfo.setTotalpage(jsonObj.optInt(TOTALPAGE_JOSANKEY));
        }
        if (jsonObj.has(COMMENTS_JSONKEY)) {    //评论列表信息
            SparseArray<DynamicCommentInfo> commnetList = new SparseArray<DynamicCommentInfo>();
            JSONArray commentArr = jsonObj.getJSONArray(COMMENTS_JSONKEY);
            for (int i = 0; i<commentArr.size(); i++) {
                JSONObject commentJsonObj = commentArr.getJSONObject(i);
                DynamicCommentInfo commentInfo = DynamicCommentInfo.parseInfo(commentJsonObj);
                if (commentInfo != null) {
                    commnetList.put(i, commentInfo);
                }
            }
            discussDetailHomeInfo.setCommentList(commnetList);
        }
        return discussDetailHomeInfo;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {

    }
}
